﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CommonModel
{
    /// <summary>
    /// 查询时的参数
    /// </summary>
    [Serializable]
    public class ParamList
    {
        #region private
        private Dictionary<string, object> vals = new Dictionary<string, object>();
        private int _page = 1;
        private int _rows = 50;
        private bool _ispage = false;
        #endregion

        /// <summary>
        /// 要取得页码 从1开始
        /// </summary>
        public int page
        {
            get
            {
                return _page;
            }
            set
            {
                _page = value;
            }
        }
        /// <summary>
        /// 每页记录数,默认10000 ,即取全部数据
        /// </summary>
        public int rows
        {
            get
            {
                return _rows;
            }
            set
            {
                _rows = value;
            }
        }
        /// <summary>
        /// 排序
        /// </summary>
        public string orderby
        {
            get;
            set;
        }
        /// <summary>
        /// 代理商Id
        /// </summary>
        public string AgentRoleId
        {
            get
            {
                return str("AgentRoleId");
            }
            set
            {
                add("AgentRoleId", value);
            }
        }
        /// <summary>
        /// 企业Id
        /// </summary>
        public string EnterpriseRoleId
        {
            get
            {
                return str("EnterpriseRoleId");
            }
            set
            {
                add("EnterpriseRoleId", value);
            }
        }


        public string EnterpriseId
        {
            get
            {
                return str("EnterpriseId");
            }
            set
            {
                add("EnterpriseId", value);
            }
        }

        /// <summary>
        /// 分页开始记录
        /// </summary>
        public int start
        {
            get
            {
                return (page - 1) * rows;
            }
        }


        /// <summary>
        /// 获取参数值 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object get(string key)
        {
            if (vals.ContainsKey(key))
            {
                return vals[key];
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 添加或设置一个参数,重复设置以最后一个为准
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public ParamList add(string key, object val)
        {
            if (key == "page")
            {
                this.page = int.Parse(val.ToString());
                return this;
            }
            else if (key == "rows")
            {
                this.rows = int.Parse(val.ToString());
                return this;
            }
            else if (key == "orderby")
            {
                this.orderby = val.ToString();
                return this;
            }
            if (vals.ContainsKey(key))
            {
                vals.Remove(key);
            }
            vals.Add(key, val);
            return this;
        }

        /// <summary>
        /// 判断不空，拼接查询条件时使用
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool isnotnull(string key)
        {
            object obj = this.get(key);
            if (obj == null)
            {
                return false;
            }
            return !string.IsNullOrWhiteSpace(obj.ToString());
        }

        /// <summary>
        /// 判断空，拼接查询条件时使用
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool isnull(string key)
        {
            object obj = this.get(key);
            if (obj == null)
            {
                return true;
            }
            return string.IsNullOrWhiteSpace(obj.ToString());
        }
        public string str(string key)
        {
            object obj = this.get(key);
            if (obj == null)
            {
                return "";
            }
            return obj.ToString().Trim();
        }

        /// <summary>
        /// 是否分页查询
        /// </summary>
        public bool ispage
        {
            get
            {
                return _ispage;
            }
            set
            {
                _ispage = value;
            }
        }

        #region  汇总相关

        /// <summary>
        /// 总计
        /// </summary>
        public string TotalColumns { get; set; }
        /// <summary>
        /// 本页小计
        /// </summary>
        public string PageTotalColumns { get; set; }
        /// <summary>
        /// 总平均
        /// </summary>
        public string TotalAvgColumns { get; set; }
        /// <summary>
        /// 本页平均
        /// </summary>
        public string PageAvgColumns { get; set; }
        #endregion
        public Dictionary<string, object> GetParamList()
        {
            return vals;
        }
        public Dictionary<string, string> GetStringParamList()
        {
            Dictionary<string, string> dic = vals.ToDictionary(kv => kv.Key, kv => Convert.ToString(kv.Value).Trim());
            dic.Add("page", this.page.ToString());
            dic.Add("rows", this.rows.ToString());
            dic.Add("orderby", this.orderby);
            dic.Add("ispage", this.ispage.ToString());
            return dic;
        }
        public static ParamList FromObject(object obj)
        {
            ParamList pl = new ParamList();
            Type t = obj.GetType();

            PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo p in pi)
            {
                MethodInfo mi = p.GetGetMethod();

                if (mi != null && mi.IsPublic)
                {
                    pl.add(p.Name, mi.Invoke(obj, new Object[] { }));
                }
            }

            return pl;
        }
    }
}
